import bidTabsTpl from './bidTabs.html';
export default function (app) {

  app.directive('bidTabs', bidTabs);

  function bidTabs() {
    'ngInject';
    return {
      restrict: 'E',
      templateUrl: bidTabsTpl,
      controller: bidTabsCtrl,
      compile: compileFn,
      scope: {
        beforeQueryFn: '=',
        queryFn: '=',
        queryBean: '=',
        queryParam: '@',
        queryType: '@',
        teamsData: '=',
        ngDisabled: '=',
        officeDisabled: '=',
      }
    };
  }

  function linkFn(scope, elements, attrs, ctrl) {
    if (scope.ngDisabled) {
      elements.parent().removeClass('tabs-main');
    }
    elements.parent().addClass('horizontal-tabs-outer');
  }

  function bidTabsCtrl($q, $rootScope, $scope, $http, paFetchService) {
    if ($scope.ngDisabled) return false;

    $scope.selectBid = function(bid, $index) {
      if (bid) {
        $rootScope.activeBid = bid;
        $rootScope.authorizationInfo.bidsInfo.forEach(function(item, index) {
          item.active = item === bid ? true : false;
        });
      } else {
        $rootScope.authorizationInfo.bidsInfo.forEach(function(item, index) {
          if (item.active) {
            $rootScope.activeBid = item;
          }
        });
      }

      $rootScope.$broadcast('bidChange', $rootScope.activeBid);
      query();
      computeTeamList();
    }

    initTabsData().then(function() {
      $scope.$watch('teamsData', function(newVal) {
        if (newVal) {
          computeTeamList();
        }
      });
    })

    function initTabsData() {
      let deferred = $q.defer();
      if ($scope.ngDisabled == 'true') {
        return false;
      }

      if ($rootScope.authorizationInfo && !$rootScope.authorizationInfo.bidsInfo) {
        paFetchService
          .getSectionList()
          .then((resp) => {
            $rootScope.authorizationInfo.generalBidsInfo = resp.data.data;
            if (!$scope.officeDisabled) {
              $rootScope.authorizationInfo.bidsInfo = $rootScope.authorizationInfo.generalBidsInfo;
              $scope.selectBid($rootScope.authorizationInfo.bidsInfo[0]);
            }

            deferred.resolve();
          });

        $http
          .get(`${SERVER_CONTEXT}/api/userTender/getTenders?role=1`)
          .then((resp) => {
            $rootScope.authorizationInfo.specialBidsInfo = resp.data.data;
            if ($scope.officeDisabled) {
              $rootScope.authorizationInfo.bidsInfo = $rootScope.authorizationInfo.specialBidsInfo;
              $scope.selectBid($rootScope.authorizationInfo.bidsInfo[0]);
            }

            deferred.resolve();
          });
      } else {
        if ($scope.officeDisabled) {
          $rootScope.authorizationInfo.bidsInfo = $rootScope.authorizationInfo.specialBidsInfo;
          chooseBaseBid();
        } else {
          $rootScope.authorizationInfo.bidsInfo = $rootScope.authorizationInfo.generalBidsInfo;
          chooseBaseBid();
        }
        query();
        deferred.resolve();
      }

      function chooseBaseBid() {
        let activeBids = $rootScope.authorizationInfo.bidsInfo.filter((item) => item.active);
        $scope.selectBid(activeBids[0] || $rootScope.authorizationInfo.bidsInfo[0]);
      }

      return deferred.promise;
    }

    function query() {
      if ($scope.queryType == 'bid') {
        $scope.queryBean[$scope.queryParam] = $rootScope.activeBid.tenderId;
      } else {
        $scope.queryBean[$scope.queryParam] = $rootScope.activeBid.orgId;
      }

      if ($scope.beforeQueryFn) {
        $scope
          .beforeQueryFn()
          .then($scope.queryFn)
      } else if ($scope.queryFn && typeof $scope.queryFn == 'function'){
        $scope.queryFn();
      }
    }

    function computeTeamList() {
      if (!$scope.teamsData) return;
      if (!$scope.teamsData._origin) {
        $scope.teamsData._origin = angular.copy($scope.teamsData);
      }

      $scope.teamsData.length = 0;
      $scope.teamsData._origin.forEach(function(team, index) {
        if (team.orgUnitId == $rootScope.activeBid.orgId) {
          $scope.teamsData.push(team);
        }
      });
    }

    function setRootBidsData(scope, bidsData) {
      if (bidsData.length) {
        scope.authorizationInfo.bidsInfo = bidsData;
      }
    }

  }


  function compileFn(tElement, tAttrs, transclude) {
    tElement.parent().addClass('tabs-main');
    return linkFn;
  }

}